home *** CD-ROM | disk | FTP | other *** search
/ Amiga Collections: Purity / Purity #23 (1994-02-10)(Diesel)(DE)[WB].zip / Purity #23 (1994-02-10)(Diesel)(DE)[WB].adf / MessagePort / AutoRemove.pas < prev    next >
Pascal/Delphi Source File  |  1994-01-20  |  8KB  |  174 lines

  1.  
  2.  
  3. {***********************************************************************}
  4. { AutoRemoveDemo.PAS                                                    }
  5. {                                                                       }
  6. { This little program demonstrates the use op MessagePorts and how to   }
  7. { use intuition with HighSpeed Pascal.                                  }
  8. { It will first check if a copy of the program is already running,      }
  9. { if NOT: Then open a window and wait for a message                     }
  10. { else  : Open a window and close both programs (the copy aswell !!)    }
  11. { So a program can be terminated by running it again...                 }
  12. {***********************************************************************}
  13. { By Hans Luyten September 1993                                         }
  14. { Original idea: Amiga Magazin 3/93 by C. Bruehann (who did this in C)  }
  15. { Compiler: HighSpeed Pascal 1.1                                        }
  16. {***********************************************************************}
  17.  
  18.  
  19.  
  20. PROGRAM AutoRemoveDemo;
  21.  
  22. uses
  23.   Exec, Intuition, Graphics;          { We need these....               }
  24.  
  25. VAR                        
  26.   MyWindow  :  tNewWindow;            { Struct for the NewWindow        }
  27.   Window    :  pWindow;               { Pointer to the NewWindow        }
  28.   
  29.   NewPort   : tMsgPort;               { MessagePort 1, a new one        }
  30.   ReplyPort : tMsgPort;               { MessagePort 2, a reply port     }
  31.   OldPort   : pMsgPort;               { Pointer to an oldport           }
  32.   MsgSend   : tMessage;               { Message to send                 }
  33.   MsgReceive: pMessage;               { Message to receive              }
  34.   Dummy     : pMessage;               { Dummy for WaitPort              }
  35.   PortName  : string;                 { PASCAL string                   }
  36.   PortCName : ARRAY [1..40] OF BYTE;  { C string                        }
  37.   NodeName  : string;                 { PASCAL string                   }
  38.   NodeCName : ARRAY [1..40] OF BYTE;  { C string                        }
  39.                 
  40.     
  41. {***********************************************************************}
  42. { OpenIntuitionLib(version);                                            }
  43. {                                                                       }
  44. { Tries to open the Intuition.library, if version=0 then ANY            }
  45. { version of intuition.library will be opened.                          }
  46. { It will return TRUE or FALSE.                                         }
  47. {***********************************************************************}  
  48. FUNCTION OpenIntuitionLib(version:INTEGER):BOOLEAN;
  49. BEGIN
  50.   IntuitionBase:=pIntuitionBase(OpenLibrary('intuition.library',version));
  51.   IF IntuitionBase=NIL THEN
  52.     OpenIntuitionLib:=FALSE           { Couldn't open intuition.library }
  53.   ELSE
  54.     OpenIntuitionLib:=TRUE;           { Yippy! Opened intuition.library }
  55. END;
  56.  
  57. {***********************************************************************}  
  58. { OpenNewWindow(....);                                                  }
  59. {                                                                       }
  60. { Tries to open a NewWindow, ALL parameters for the NewWindowStruct     }
  61. { are passed to OpenNewWindow !                                         }
  62. { It will return the pWindow pointer.                                   }
  63. { TitleMode is TRUE if you USE a title, and FALSE if you don't !!       }
  64. { If you don't use a title then enter ANY kind of string, ie. ''.       }
  65. {***********************************************************************}  
  66. FUNCTION OpenNewWindow(WLeftEdge,WTopEdge,WWidth,WHeight: INTEGER;
  67.                        WDetailPen,WBlockPen: shortint;
  68.                        WIDCMPFlags,WFlags: long;
  69.                        WFirstGadget: pGadget;
  70.                        WCheckMark: pImage;
  71.                        WTitle: string;
  72.                        WScreen: pScreen;
  73.                        WBitMap: pBitMap;
  74.                        WMinWidth,WMinHeight: INTEGER;
  75.                        WMaxWidth,WMaxHeight,WType_: word;
  76.                        TitleMode:BOOLEAN):pWindow;
  77. VAR TempWindow  :  tNewWindow;
  78.     TempTitle   :  ARRAY [1..80] OF byte;
  79. BEGIN
  80.   IF TitleMode THEN                   { Title to C-format string        }
  81.     PasToC(WTitle,TempTitle);
  82.   WITH TempWindow DO
  83.     BEGIN
  84.       LeftEdge    :=WLeftEdge;        { Look at the AutoDocs or         }
  85.       TopEdge     :=WTopEdge;         { intuition unit for more details }
  86.       Width       :=WWidth;
  87.       Height      :=WHeight;
  88.       DetailPen   :=WDetailPen;
  89.       BlockPen    :=WBlockPen;
  90.       IDCMPFlags  :=WIDCMPFlags;
  91.       Flags       :=WFlags;
  92.       FirstGadget :=WFirstGadget;
  93.       CheckMark   :=WCheckMark;
  94.       Title       :=@TempTitle;        { POINTER to the title string    }
  95.       Screen      :=WScreen;
  96.       BitMap      :=WBitMap;
  97.       MinWidth    :=WMinwidth;
  98.       MinHeight   :=WMinHeight;
  99.       MaxWidth    :=WMaxWidth;
  100.       MaxHeight   :=WMaxHeight;
  101.       Type_       :=WType_;
  102.     END;
  103.     IF NOT(TitleMode) THEN
  104.       TempWindow.Title:=NIL;           { No title used...               }
  105.     OpenNewWindow := OpenWindow(@TempWindow);  
  106. END;
  107.  
  108. {***********************************************************************}  
  109. { Main                                                                  }
  110. {***********************************************************************}  
  111. BEGIN
  112.   IF OpenIntuitionLib(0) THEN           { Try to open intuition.library }
  113.     BEGIN
  114.       PortName:='AutoRemove Port';      { Our Port's name...            }
  115.       PasToC(PortName,PortCName);       { Convert it to C-String        }
  116.       OldPort:=FindPort(@PortCName);    { Look if it is already there   }
  117.       IF (OldPort<>NIL) THEN            { <>NIL = Found our port        }
  118.         BEGIN
  119.           Window:=OpenNewWindow(100,150,300,45,2,3,ACTIVEWINDOW,
  120.                                  SMART_REFRESH OR NOCAREREFRESH,
  121.                                  NIL,NIL,'^ AutoRemove Already Started',
  122.                                  NIL,NIL,10,10,640,256,WBENCHSCREEN,TRUE);
  123.       
  124.           NodeName:='AutoRemove Reply'; { Create our new port...        }
  125.           PasToC(NodeName,NodeCName);   { and convert it to C-String    }
  126.       
  127.           ReplyPort.mp_Node.ln_Pri:=0;  { Init this new port            }
  128.           ReplyPort.mp_Node.ln_Name:=@NodeCName;
  129.           ReplyPort.mp_SigTask:=pTask(FindTask(NIL));
  130.           AddPort(@ReplyPort);          { Add this new port             }
  131.       
  132.           MsgSend.mn_length:=SizeOf(tMessage);  { Init a message        }
  133.           MsgSend.mn_Node.ln_Type:=NT_MESSAGE;  
  134.           MsgSend.mn_ReplyPort:=@ReplyPort;
  135.           PutMsg(OldPort,@MsgSend);     { Send it to the oldport...     }
  136.       
  137.           MsgReceive:=WaitPort(@ReplyPort); { Wait for respons..        }
  138.           RemPort(@ReplyPort);          { Remove the oldport            }
  139.           Delay(2000);
  140.           CloseWindow(window);
  141.         END
  142.       ELSE
  143.         BEGIN 
  144.           Window:=OpenNewWindow(100,100,300,45,2,3,ACTIVEWINDOW,
  145.                                 SMART_REFRESH OR NOCAREREFRESH,
  146.                                 NIL,NIL,'AutoRemove Started',NIL,NIL,
  147.                                 10,10,640,256,WBENCHSCREEN,TRUE);
  148.           IF Window<>NIL THEN                       { Window opened ??? }
  149.             BEGIN  
  150.               NewPort.mp_Node.ln_Pri:=0;      { Init this port          }
  151.               NewPort.mp_Node.ln_Name:=@PortCName;
  152.               NewPort.mp_SigTask:=pTask(FindTask(NIL));
  153.               AddPort(@NewPort);              { Add this port           }
  154.               
  155.               Dummy:=WaitPort(@NewPort);      { Wait for a message      }
  156.               MsgReceive:=GetMsg(@NewPort);     { Get the message       }
  157.               ReplyMsg(MsgReceive);           { Reply to message        }
  158.               RemPort(@NewPort);              { Remove this port        }
  159.               Delay(2000);   { Small delay, so you'll notice the action }
  160.               CloseWindow(Window);            { Close the window        }
  161.             END;
  162.         END;
  163.       CloseLibrary(pLibrary(IntuitionBase));  { Close LIBS !!           }
  164.     END;
  165. END.  
  166.  
  167.  
  168.  
  169.  
  170.  
  171.  
  172.  
  173.  
  174.